/**
 * UserController
 *
 * @description :: Server-side actions for handling incoming requests.
 * @help        :: See https://sailsjs.com/docs/concepts/actions
 */

 const BlockChainApp = require("../services/BlockChainApp");
const CaseReportContract = require("../services/contracts/CaseReportContract");
 const { totalUser } = require("../services/contracts/UserContract");
 const UserContract = require("../services/contracts/UserContract");
 const Message = require("../services/Message");
 
 // const Message = require("../services/Message")
 
 module.exports = {
   // 新增病历记录
   createRecord: async function(req, res){
        var param = req.body.data
        console.log("new record: ", param)
        // var instance = await BlockChainApp.contracts.CaseRecords.deployed()
        CaseReportContract.addCaseReport(param)
        .then(
            data=>{
                return res.json(Message.success("新增病历记录完成"))
            }
        )
        .catch(
            err=>{
                console.log(err)
                return res.json(Message.error("新增病历信息错误"))
            }
        )
    },

    // 查看所有病历记录
    getAllCaseRecord: async function(req, res){
        // var instance = await BlockChainApp.contracts.CaseRecords.deployed()

        CaseReportContract.getAllCaseRecords()
        .then(
            data=>{
                return res.json(Message.success(data))
            }
        )
        .catch(
            err=>{
                return res.json(Message.error("获取病历信息记录出错"))
            }
        )
    },

    // 条件查询病历信息
    getCaseRecordWithCondition: async function(req, res){
        var param = req.body.data
        CaseReportContract.findCaseRecordWithCondition(param)
        .then(data=>{
            return res.json(Message.success(data))
        })
        .catch(error=>{
            return res.json(Message.error("获取病历信息记录出错"))
        })
    },

    // 更新病历信息
    updateCaseRecord: async function(req, res){
        var param = req.body.data
        console.log("更新病历信息：", param.query)
        CaseReportContract.updateCaseRecord(param)
        .then(data=>{
            console.log("病历更新完成：", data)
            return res.json(Message.success(data))
        })
        .catch(error=>{
            console.log("error: ", error)
            return res.json(Message.error("更新病历数据错误"))
        })
    },

    // 查询病人过往病史
    findClientHistoryInfo: async function(req, res){
        // var param = req.body.data;
        console.log("param:", req.query);
        var currentUser = req.session.authenticated
        var contract = await BlockChainApp.contracts.CaseRecords.deployed();
        var histories = await contract.findCaseHistories.call(req.query.username, {from: currentUser.accountAddr});
        
        // 查询病人病历对应的处方信息
        var prescriptionContract = await BlockChainApp.contracts.Prescription.deployed();
        for(var i = 0; i < histories.length; i++){
            var history = histories[i];
            console.log("history: ", history);
            history[6] = await prescriptionContract.findCasePriscription.call(history[0], {from: currentUser.accountAddr})
        }
        console.log("病人历史记录信息", histories);
        return res.json(Message.success(histories));
    },
 };
 
 